home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5449 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gets(rec->num);  I don't know what I am doing wrong...
  5. Date: 9 Feb 1996 07:55 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <9FEB199607551645@erich.triumf.ca>
  9. References: <4fempt$mjg@aphex.direct.ca>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4fempt$mjg@aphex.direct.ca>, etoivane@direct.ca (Ed Toivanen) writes...
  14. >I posted the code that I wrote so far.  I can't make gets(rec->id); work 
  15. >properly,  I get 6 or 7 compilation errors indicating that parameter 1 does not 
  16. >match function prototype.  Each gets() call is incorrect! What to do? 
  17.  
  18. First, gets() is evil, and should be avoided at all costs - use fgets()
  19. instead.   gets() allows the user to enter as many chars as he wants, possibly
  20. writing well beyond the space you have allowed for his entry, overwriting other
  21. variables, return addresses on the stack, etc.
  22.  
  23.    <snip>
  24. >typedef struct student_record{
  25. >    int id;                     /* Range: 1..99, Unique */
  26. >    char name[NAME_LEN + 1];    /* Non-unique */
  27. >    PROGRAM major;
  28. >    MARK marks;
  29. >} STUDENT_RECORD;
  30.   <more snips>
  31.  
  32. >bool addRecord(FILE* fp, STUDENT_RECORD* rec){
  33. >    printf("Student id\n");
  34. >    gets(rec->id);
  35.  
  36. rec->id is an int, and gets() (or fgets()) only gets strings.  Use fgets() to
  37. get the ID into a temporary string, then atoi() to convert it to an int.
  38.  
  39. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  40. Internet: bennett@triumf.ca         | of one another only when one can be
  41. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  42. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  43. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.